package com.tuzhi.base.web.controller;

import com.alibaba.fastjson2.JSON;
import com.tuzhi.base.jpa.domain.Pager;
import com.tuzhi.base.service.BaseService;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.base.web.vo.PageVO;
import com.tuzhi.common.constants.ResultConstant;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.common.helper.ResultHelper;
import com.tuzhi.util.ReflectionTzUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

public abstract class SimpleBaseController<T, Pk> {

    private static final Logger log = LoggerFactory.getLogger(SimpleBaseController.class);

    protected abstract BaseService getThisService();


    @PutMapping("/save")
    public ResultBO save(@RequestBody T entity) {
        if (entity == null) {
            log.warn("传入参数为空!");
            return ResultHelper.build(ResultConstant.ERR_PARAM_NULL);
        }
        log.info(entity.toString());
        getThisService().save(entity);
        return ResultHelper.success();
    }


    @DeleteMapping("del/{id}")
    public ResultBO del(@PathVariable(value = "id", required = true) Pk id) {
        getThisService().deleteById(id);
        return ResultHelper.success();
    }


    protected T transEntity(Object obj) {
        Class t = ReflectionTzUtil.getClassT(this, 0);

        if (obj != null && obj instanceof Map) {
            String json = JSON.toJSONString(obj);
            return (T) JSON.parseObject(json, ReflectionTzUtil.getClassT(this, 0));
        } else {
            return (T) ReflectionTzUtil.newInstance(t, new Class[0], null);
        }
    }

    @GetMapping("/get/{pk}")
    public AjaxResultVO get(@PathVariable("pk") Pk pk) {
        log.info("get:{}",pk);
        T t = (T) getThisService().findById(pk);
        return AjaxResultHelper.success().ofData(t);

    }


    @PostMapping({"/pager"})
    public AjaxResultVO pager(@RequestBody(required = false) PageVO pageVO) {
        AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
        Pager<T> pager = new Pager<>();
        pager.setPage(pageVO.getPage());
        pager.setSize(pageVO.getSize());
        pager = pagerVoCreateWhere(pageVO, pager);
        pager = getThisService().listPager(pager);
        pageVO.setData(pager.getData());
        pageVO.setTotalPage(pager.getTotalPage());
        pageVO.setTotalNum(pager.getTotalNum());
        ajaxResultVO.setData(pageVO);
        return ajaxResultVO;
    }

    /**
     * 把前端传的查询条件转成查询条件
     *
     * @param pageVO
     * @param pager
     */
    protected Pager<T> pagerVoCreateWhere(PageVO pageVO, Pager<T> pager) {
        return pager;
    }

    /**
     * 把请求的JSON对象转成对应的VO
     *
     * @param reqData
     * @param eClass
     * @param <E>
     * @return
     */
    protected <E> E pageReqData2Bean(Object reqData, Class<E> eClass) {
        String json = JSON.toJSONString(reqData);
        E vo = JSON.parseObject(json, eClass);
        return vo;
    }

    /**
     * 心跳接口查询
     *
     * @return
     */
    @GetMapping("/test")
    public AjaxResultVO test() {
        log.info("心跳接口测试，接口正常");
        AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
        ajaxResultVO.setMsg("心跳接口测试，接口正常:");
        ajaxResultVO.setData(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        return ajaxResultVO;
    }



}
